package com.stevin.tool.utils;

import javassist.CtClass;
import javassist.CtMethod;
import javassist.Modifier;

/**
 * Created by mars on 04/11/2018.
 */
public class ClassPermissionUtils {


    public static boolean isThirdPartyClass(String className) {
        if (isRootClass(className)) {
            return true;
        }

        if (isIntellijClass(className)) {
            return true;
        }
        if (isLog4jClass(className)) {
            return true;
        }
        return false;
    }

    public static boolean isFrozen(int modifiers) {
        if (Modifier.isAbstract(modifiers)) {
            return true;
        }
        if (Modifier.isNative(modifiers)) {
            return true;
        }
        if (Modifier.isFinal(modifiers)) {
            return true;
        }
        if (Modifier.isInterface(modifiers)) {
            return true;
        }
        if (Modifier.isProtected(modifiers)) {
            return true;
        }
        if (Modifier.isAnnotation(modifiers)) {
            return true;
        }
        return false;
    }


    public static boolean isAbstract(CtMethod method) {
        return Modifier.isAbstract(method.getModifiers());
    }

    public static boolean isFinal(CtMethod method) {
        return Modifier.isFinal(method.getModifiers());
    }

    public static boolean isNative(CtMethod method) {
        return Modifier.isNative(method.getModifiers());
    }

    public static boolean isNativeMethod(String methodName) {
        if (methodName.equalsIgnoreCase("equals")) {
            return true;
        }
        if (methodName.equalsIgnoreCase("wait")) {
            return true;
        }
        if (methodName.equalsIgnoreCase("finalize")) {
            return true;
        }
        if (methodName.equalsIgnoreCase("printStackTrace")) {
            return true;
        }
        if (methodName.equalsIgnoreCase("getSuppressed")) {
            return true;
        }
        if (methodName.equalsIgnoreCase("getCause")) {
            return true;
        }
        if (methodName.equalsIgnoreCase("getStackTrace")) {
            return true;
        }
        if (methodName.equalsIgnoreCase("getLogger")) {
            return true;
        }
        if (methodName.equalsIgnoreCase("toString")) {
            return true;
        }
        return false;
    }

    private static boolean isRootClass(String className) {
        if (className == null) {
            return true;
        }
        if (className.startsWith("java")) {
            return true;
        }
        if (className.startsWith("sun")) {
            return true;
        }
        if (className.startsWith("com/sun")) {
            return true;
        }
        if (className.startsWith("javax")) {
            return true;
        }
        return false;
    }

    private static boolean isLog4jClass(String className) {
        if (className.startsWith("org/apache/log4j")) {
            return true;
        }
        if (className.contains("Logger")) {
            return true;
        }
        return false;
    }

    private static boolean isIntellijClass(String className) {
        if (className.startsWith("com/intellij")) {
            return true;
        }
        if (className.startsWith("org/jetbrains")) {
            return true;
        }
        return false;
    }

}
